package modules.manager
{
	import com.feng.FUI;
	
	import flash.display.MovieClip;
	
	import br.com.stimuli.loading.loadingtypes.ImageItem;
	
	import modules.GameDispatcher;
	import modules.GameEvent;
	import modules.load.Load;
	import modules.lobby.Lobby;
	
	import utils.ResourceManager;

	public class ErrorUIManager
	{
		public function ErrorUIManager()
		{
			addListeners();
		}

		private static var instance:ErrorUIManager;

		/**
		 * 关闭界面触发函数
		 */
		private var closeFunc:Function;

		public static function init():void
		{
			logger("提示警告框模块初始化");

			if (instance == null)
			{
				instance = new ErrorUIManager();
			}
		}

		private var _errorUI:MovieClip;

		private function get errorUI():MovieClip
		{
			if (_errorUI == null)
			{
				var viewItem:ImageItem = Load.loader.get(Lobby.viewPath) as ImageItem;
				var WaitViewCla:Class = viewItem.getDefinitionByName("fla.view.lobby.WaitView") as Class;
				_errorUI = new WaitViewCla();
				
			}
			return _errorUI;
		}

		private function get dispatcher():GameDispatcher
		{
			return GameDispatcher.instance;
		}

		/**
		 * 添加侦听器
		 */
		private function addListeners():void
		{
			dispatcher.addEventListener(GameEvent.SHOW_WARN_INFO, onWarnInfo);

			dispatcher.addEventListener(GameEvent.SHOW_WAIT_INFO, onWaitInfo);

			dispatcher.addEventListener(GameEvent.SHOW_INFO, onShowInfo);
		}

		/**
		 * 弹出警告框
		 */
		public function onShowInfo(event:GameEvent):void
		{
			//数据为空表示需要关闭提示框
			if (event.data == null)
			{
				closeViewFunc();
			}
			else
			{
				showViewFunc();
				if (event.data is String)
				{
					showInfoUI(event.data);
				}
				if (event.data is Object)
				{
					var data:Object = event.data;
					showInfoUI(data.info);
					if (data.closeFunc != null)
					{
						closeFunc = data.closeFunc;
					}
				}
			}
		}

		/**
		 * 弹出警告框
		 */
		public function onWarnInfo(event:GameEvent):void
		{
			//数据为空表示需要关闭提示框
			if (event.data == null)
			{
				closeViewFunc();
			}
			else
			{
				showViewFunc();
				showWarnUI(event.data);
			}
		}

		/**
		 * 弹出等待框
		 */
		public function onWaitInfo(event:GameEvent):void
		{
			//数据为空表示需要关闭提示框
			if (event.data == null)
			{
				closeViewFunc();
			}
			else
			{
				showViewFunc();

				showWaitErrorUI(event.data);
				errorUI.showWaitInfoText.text = ResourceManager.getInstance().getString('MahjongResource', 'errorUIWaitInfo') + "."
			}
		}

		/**
		 * 显示界面
		 **/
		private function showViewFunc():void
		{
			UIAllRefer.infoLayer.addChild(errorUI);
			errorUI.x = (UIAllRefer.stage.stageWidth - errorUI.width) / 2;
			errorUI.y = (UIAllRefer.stage.stageHeight - errorUI.height) / 2;
		}

		/**
		 * 关闭界面
		 **/
		private function closeViewFunc():void
		{
			if (errorUI.parent)
			{
				if (closeFunc != null)
				{
					closeFunc();
					closeFunc = null;
				}
				errorUI.parent.removeChild(errorUI);
			}
		}

		private function reset():void
		{
			errorUI.infoLvlClickBtn.visible = false;
			errorUI.warnBtn.visible = false;
			errorUI.exitBtn.visible = false;
			errorUI.errorText.visible = false;
			errorUI.infoText.visible = false;
			errorUI.refreshText.visible = false;
			errorUI.showWaitInfoText.visible = false;
			errorUI.exitText.visible = false;
		}

		public function showErrorUI(errorTxt:String):void
		{
			reset();
			
			errorUI.errorText.text = errorTxt;
			errorUI.errorText.visible = true;
			
			errorUI.refreshText.visible = true;
			errorUI.refreshText.text = ResourceManager.getInstance().getString('MahjongResource', 'refreshInfo');
		}

		public function showWaitErrorUI(errorTxt:String):void
		{
			reset();
			
			errorUI.errorText.text = errorTxt;
			errorUI.errorText.visible = true;
		}

		public function showWarnUI(errorTxt:String):void
		{
			reset();
			
			errorUI.errorText.text = errorTxt;
			errorUI.errorText.visible = true;
			errorUI.warnBtn.visible = true;
		}

		public function showInfoUI(infoTxt:String):void
		{
			reset();
			
			errorUI.infoText.text = infoTxt;
			errorUI.infoText.visible = true;
			errorUI.infoLvlClickBtn.visible = true;
		}

		public function showDirectInfoUI(infoTxt:String):void
		{
			reset();
			
			errorUI.infoText.text = infoTxt;
			errorUI.infoText.visible = true;
			errorUI.infoLvlClickBtn.visible = true;
		}

		public function showExitUI(infoTxt:String):void
		{
			reset();
			
			errorUI.exitBtn.visible = true;
			errorUI.exitText.text = infoTxt;
			errorUI.exitText.visible = true;
		}
	}
}
